home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-08-27 | 2.0 KB | 38 lines | [ttro/ttxt] |
- This is a game of Othello, also known as Reversi. No computer
- "intelligence" is involved; the computer chooses its moves at random.
- (That's the only way I can ever win, and even so I lose most of the time!)
- So it's a "dumb" Othello, which is why I call it Odummo.
-
- The challenge here was to implement the rules of the game: the computer
- must know whether the user's proposed move is legal, must know what all its
- own legal moves are, and when it or the user makes a move, must "flip" the
- correct pieces.
-
- At first, I expected this to be an arduous programming task. Let's say the
- user clicks an empty square. It's legal to move there if there is an
- adjacent square with the computer's color piece on it, which leads,
- continuing in the same direction, either to a piece of the user's color or
- to another piece with the computer's color which leads, continuing in the
- same direction... And so on. It sounds like a mess. It sounds as if I'm
- going to need some sort of supplementary array of legal moves, which will
- have to be examined with loops or recursion or something.
-
- But not at all! REALBasic is object-based. So, let every square be an
- object. Now the collection of squares (the playing surface itself) *is* my
- array. And since they are objects, squares can query one another. Thus, the
- square where the user has clicked only has to query the squares adjacent to
- it: "Have you got a piece that's the computer's color? You do? Well then,
- do you lead to a piece that's the user's color?" Now it's up to the square
- being queried to answer this question, by itself querying the *next* square
- in the same direction - and so on. What any *one* square has to know how to
- do turns out to be extremely simple.
-
- The result was that in the end it took me considerably less than a 24-hour
- period to write the program - and much of that time was spent in other
- activities such as sleeping, eating, reading email, doing a laundry at the
- laundromat, and going for a run. To me, that's a real testimony to the
- power of the REALBasic object model.
-
- Matt Neuburg
- matt@tidbits.com
- http://www.tidbits.com